package edu.uoc.tdp.client.conexio;

import edu.uoc.tdp.beans.conexio.Usuari;
import edu.uoc.tdp.beans.proves.Prova;
import edu.uoc.tdp.common.*;
import java.rmi.RemoteException;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Grup TDP 1X4J
 */
public class PantallaVisioProves extends javax.swing.JDialog {

    private RemotEstudiantsInterface serveiRMIEstudiants;
    private Usuari usuEst;
    
    /**
     * Creates new form PantallaVisioProves
     */
    public PantallaVisioProves(java.awt.Frame parent, boolean modal,
            Usuari usuProves,
            RemotEstudiantsInterface serveiRMIEstudiants) throws EOlympicsException {

        super(parent, modal);

        TDSLanguageUtils.setDefaultLanguage("edu/uoc/tdp/i18n/messages");
        initComponents();
        
        usuEst = usuProves;
        this.serveiRMIEstudiants = serveiRMIEstudiants;
        
        this.setLocationRelativeTo(parent);
        
        setTitle(TDSLanguageUtils.getMessage("titol.general") + " - "
                + TDSLanguageUtils.getMessage("connexio.pantalla.visproves.titol"));

        BotoSortir.setText(TDSLanguageUtils.getMessage("connexio.pantalla.visproves.btnSortir"));

        for (int i = 0; i < TaulaProves.getColumnCount(); i++) {
            TaulaProves.getColumnModel().getColumn(i).setHeaderValue(TDSLanguageUtils.getMessage("connexio.pantalla.visproves.taula.col" + i));
        }
        
        CarregaReixeta();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BotoSortir = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        TaulaProves = new javax.swing.JTable();
        jLabTitol = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        BotoSortir.setText("Sortir");
        BotoSortir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotoSortirActionPerformed(evt);
            }
        });

        TaulaProves.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nom de la Prova", "Data", "Realitzada (Si/No)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(TaulaProves);
        TaulaProves.getColumnModel().getColumn(0).setResizable(false);
        TaulaProves.getColumnModel().getColumn(0).setPreferredWidth(150);
        TaulaProves.getColumnModel().getColumn(1).setResizable(false);
        TaulaProves.getColumnModel().getColumn(1).setPreferredWidth(37);
        TaulaProves.getColumnModel().getColumn(2).setResizable(false);
        TaulaProves.getColumnModel().getColumn(2).setPreferredWidth(65);

        jLabTitol.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabTitol.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabTitol.setText("Proves Inscrites Estudiant");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(BotoSortir, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jLabTitol, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(26, Short.MAX_VALUE)
                .addComponent(jLabTitol)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(BotoSortir, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CarregaReixeta() {
        try {
            // Borrem la taula
            while (TaulaProves.getRowCount() > 0) {
                ((DefaultTableModel) TaulaProves.getModel()).removeRow(0);
            }

            List<Prova> inscripcions =
                    serveiRMIEstudiants.ProvesAmbInscripcio(usuEst.getIdIntern());
            
            if (inscripcions.isEmpty()) {
                JOptionPane.showMessageDialog(null,
                        TDSLanguageUtils.getMessage("connexio.pantalla.visproves.missatge.nohihaprv"),
                        TDSLanguageUtils.getMessage("connexio.pantalla.pantmiss.titol"), JOptionPane.INFORMATION_MESSAGE);
            } else {
                int fila = 0;
                for (Prova prv : inscripcions) {
                    // Afegim una fila a la taula.
                    ((DefaultTableModel) TaulaProves.getModel()).addRow(new Object[]{""});
                    // Carregar les dades a la fila corresponent de la taula
                    TaulaProves.setValueAt(prv.getDescripcio(), fila, 0);
                    TaulaProves.setValueAt(EOlympicsUtils.getStringDate(serveiRMIEstudiants.ObtenirDataInscripcio(prv.getIdProva(), usuEst.getIdIntern())), fila, 1);
                    if (serveiRMIEstudiants.ComprovaParticipacio(prv.getIdProva(), "E", usuEst.getIdIntern())
                            | serveiRMIEstudiants.ComprovaParticipacio(prv.getIdProva(), "F", usuEst.getIdIntern())) {
                        TaulaProves.setValueAt("Si", fila, 2);
                    } else {
                        TaulaProves.setValueAt("No", fila, 2);
                    }
                    fila++;
                }
            }

        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog
                    (null, TDSLanguageUtils.getMessage("ERR_REMOT_VISUUSU"), "Error", JOptionPane.ERROR_MESSAGE);
        } catch (EOlympicsException ex) {
            JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("ERR_VISUUSU"), "Error", JOptionPane.ERROR_MESSAGE);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        
    }

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        dispose();
    }//GEN-LAST:event_formWindowClosing

    private void BotoSortirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotoSortirActionPerformed
        dispose();
    }//GEN-LAST:event_BotoSortirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotoSortir;
    private javax.swing.JTable TaulaProves;
    private javax.swing.JLabel jLabTitol;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
